<?php

namespace App\Http\Controllers\admin;

use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use App\Admin;
use App\Post;
use App\User;
class AuthController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data = DB::table('admins')->get();
        return view('admin.upms.index', compact('data'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */


    public function store(Request $request)
    {

        DB::beginTransaction(); //事务
        try {

            $admins = Admin::firstOrCreate(
                [
                    'name' => $request->name,
                ],
                [
                    'auid' => $request->auid,
                    'name' => $request->name,
                    'password' => bcrypt($request->password),
                ]);

            if ($admins->wasRecentlyCreated) {

                $user = new User();
                $user->id = $admins->auid;//权限id
                $user->aid = $admins->id;//用户id
                $user->name = $admins->name;
                $user->save();

                $post = new Post;
                $post->aid = $user->aid;//用户id
                $post->user_id = $admins->auid;//权限id
                $post->save();

            }

            if ($user && $post && $admins->wasRecentlyCreated) {
                DB::commit();
                return redirect('camelredadmin/userAuth/userAuthorities');
            }
        } catch (\Exception $e) {
            DB::rollBack();
            echo $e;
            echo " <script language=javascript>alert('添加失败！检查该用户是否存在！');window.window.location.href='/admin/userAuth/userAuthorities';</script> ";

        }

    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {

            $post1 = DB::table('admins')->where('id', $id)->delete();
            $post2 = DB::table('users')->where('aid', $id)->delete();
            $post3 = DB::table('posts')->where('aid', $id)->delete();
            return  $data = ['status' => 0,'msg' => '删除成功！'];

    }
}
